package net.lukecloud.xile.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import net.lukecloud.xile.R;
import net.lukecloud.xile.fragment.OrderFragment;
import net.lukecloud.xile.laundry.LaundryFragment;
import net.lukecloud.xile.fragment.MeFragment;

public class MainActivity extends AppCompatActivity {


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {


        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    if(mCurrentFragment!=mLaundryFragment){
                        if(mLaundryFragment==null){
                            mLaundryFragment=new LaundryFragment();
                        }
                        mCurrentFragment=mLaundryFragment;
                        getSupportFragmentManager().beginTransaction().replace(R.id.content, mCurrentFragment).commit();
                    }

                    return true;
                case R.id.navigation_dashboard:
                    if(mCurrentFragment!=mOrderFragment){
                        if(mOrderFragment==null){
                            mOrderFragment=new OrderFragment();
                        }
                        mCurrentFragment=mOrderFragment;
                        getSupportFragmentManager().beginTransaction().replace(R.id.content, mCurrentFragment).commit();
                    }
                    return true;
                case R.id.navigation_me:

                    if(mCurrentFragment!=mMeFragment){
                        if(mMeFragment==null){
                            mMeFragment=new MeFragment();
                        }
                        mCurrentFragment=mMeFragment;
                        getSupportFragmentManager().beginTransaction().replace(R.id.content, mCurrentFragment).commit();
                    }
                    return true;
            }
            return false;
        }

    };


    Fragment mCurrentFragment, mMeFragment,mLaundryFragment,mOrderFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        mCurrentFragment= mLaundryFragment=new LaundryFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.content, mCurrentFragment).commit();
    }

}
